import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:oktoast/oktoast.dart';
import 'package:school_safe_teacher/resourse/app_colors.dart';
import 'package:school_safe_teacher/widget/my_widget.dart';
import 'package:school_safe_teacher/widget/no_message_widget.dart';

import 'action.dart';
import 'state.dart';

Widget buildView(
    studentsReviewState state, Dispatch dispatch, ViewService viewService) {
  return body(
      child: Container(
        color: AppColors.MainBackColor,
        child: Column(children: [
          Container(
            child: myBar(context: viewService.context, title: "学生审核"),
            color: Colors.white,
          ),
          Container(
            child: Container(
                height: 50,
                color: Colors.white,
                child: DefaultTabController(
                  length: 2,
                  initialIndex: state.type,
                  child: TabBar(
                    unselectedLabelColor: AppColors.middleTextColor,
                    unselectedLabelStyle: TextStyle(fontSize: 12),
                    labelColor: AppColors.deepTextColor,
                    labelStyle:
                        TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
                    isScrollable: false,
                    indicatorColor: Color(0xffffdc21),
                    indicatorSize: TabBarIndicatorSize.label,
                    tabs: [
                      Text("学生审核"),
                      Text("住校生管理"),
                    ],
                    onTap: (int index) {
                      state..type = index;
                      // dispatch(studentsReviewActionCreator.onRefresh());
                      state.easyRefreshController.callRefresh();
                    },
                  ),
                )),
          ),
          Expanded(
              child: EasyRefresh(
                  controller: state.easyRefreshController,
                  header: MaterialHeader(
                      completeDuration: Duration(milliseconds: 0)),
                  footer: MaterialFooter(),
                  firstRefresh: true,
                  enableControlFinishRefresh: true,
                  enableControlFinishLoad: true,
                  onRefresh: () async {
                    //下拉刷新
                    state..page = 1;
                    dispatch(studentsReviewActionCreator.onRefresh());
                  },
                  onLoad: () async {
                    //上拉加载
                    state..page = state.page + 1;
                    dispatch(studentsReviewActionCreator.onload());
                  },
                  child: _list(state, dispatch, viewService)))
        ]),
      ),
      context: viewService.context);
}

Widget _list(
    studentsReviewState state, Dispatch dispatch, ViewService viewService) {
  if (state.items.isNotEmpty) {
    return ListView.builder(
      padding: EdgeInsets.zero,
      itemBuilder: (BuildContext contentext, int index) {
        return Container(
          padding: EdgeInsets.only(top: 10, bottom: 10, left: 15, right: 15),
          margin: EdgeInsets.only(top: 5, bottom: 5, left: 15, right: 15),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.all(Radius.elliptical(10, 10)),
            color: Colors.white,
          ),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              Row(children: [
                Text(
                  state.items[index].memberName,
                  style: TextStyle(
                      color: AppColors.deepTextColor,
                      fontSize: 15,
                      fontWeight: FontWeight.bold),
                ),
                Expanded(child: SizedBox()),
                // GestureDetector(child: Image.asset("images/student_delete@2x.png",width: 15,height: 15,),onTap: (){
                //
                // },)
              ]),
              Divider(
                height: 15,
                color: AppColors.MainBackColor,
              ),
              Container(
                child: Row(
                  children: [
                    Image.asset(
                      "images/student_choice@2x.png",
                      width: 5,
                      height: 5,
                    ),
                    Container(
                      margin: EdgeInsets.only(left: 5),
                      child: Text(
                        "身份证号：" + state.items[index].idCard,
                        style: TextStyle(
                            color: AppColors.middleTextColor, fontSize: 14),
                      ),
                    )
                  ],
                ),
              ),
              Container(
                margin: EdgeInsets.only(top: 15),
                child: Row(
                  children: [
                    Image.asset(
                      "images/student_choice@2x.png",
                      width: 5,
                      height: 5,
                    ),
                    Container(
                      margin: EdgeInsets.only(left: 5),
                      child: Text(
                        "宿舍信息：" + state.items[index].dorm,
                        style: TextStyle(
                            color: AppColors.middleTextColor, fontSize: 14),
                      ),
                    )
                  ],
                ),
              ),
              state.type == 0
                  ? Container(
                      margin: EdgeInsets.only(top: 10),
                      child: Row(
                        children: [
                          Expanded(child: Text("")),
                          GestureDetector(
                            child: Container(
                              margin: EdgeInsets.only(right: 15),
                              width: 64,
                              height: 28,
                              decoration: BoxDecoration(
                                borderRadius: BorderRadius.all(
                                  Radius.elliptical(10, 10),
                                ),
                                color: Color(0xffff9100),
                              ),
                              alignment: Alignment.center,
                              child: Text("同意",
                                  style: TextStyle(
                                      color: Colors.white, fontSize: 14),
                                  textAlign: TextAlign.center),
                            ),
                            onTap: () {
                              showToast("点击了同意");
                              dispatch(studentsReviewActionCreator.onreview(
                                  1, state.items[index].memberId.toString()));
                            },
                          ),
                          GestureDetector(
                            child: Container(
                              width: 64,
                              height: 28,
                              decoration: BoxDecoration(
                                  borderRadius: BorderRadius.all(
                                      Radius.elliptical(10, 10)),
                                  border: Border.all(
                                      color: Color(0xffff9100), width: 1),
                                  color: Colors.white),
                              alignment: Alignment.center,
                              child: Text("拒绝",
                                  style: TextStyle(
                                      color: Color(0xffff9100), fontSize: 14),
                                  textAlign: TextAlign.center),
                            ),
                            onTap: () {
                              showToast("点击了拒绝");
                              dispatch(studentsReviewActionCreator.onreview(
                                  -1, state.items[index].memberId.toString()));
                            },
                          )
                        ],
                      ),
                    )
                  : Container()
            ],
          ),
        );
      },
      itemCount: state.items.length,
    );
  } else {
    return noMemberWidget();
  }
}
